package com.yinshan.voiceplay

import android.text.TextUtils
import java.util.regex.Pattern

/**
 * @author limh
 * @function 字符串操作工具类
 * @date 2021/5/21 10:49
 */
object MoneyUtils {

    private val NUM = charArrayOf('0', '1', '2', '3', '4', '5', '6', '7', '8', '9')
    private val CHINESE_UNIT = charArrayOf(
        '元',
        '拾',
        '佰',
        '仟',
        '万',
        '拾',
        '佰',
        '仟',
        '亿',
        '拾',
        '佰',
        '仟'
    )

    /**
     * 添加商户前缀音频文件
     */
    fun getSuffixVoice(prefix: String?): MutableList<Int>? {
        if (!TextUtils.isEmpty(prefix) && prefix!!.startsWith("商户")) {
            val prefixVoices = mutableListOf<Int>()
            prefixVoices.add(R.raw.prefix_shop)
            val prefixStr = prefix.substring(2)
            for (i in prefixStr.indices){
                if (prefixStr[i] in '0'..'9'){
                    prefixVoices.add(getNumRaw((prefixStr[i] - 48).toInt()))
                }
            }
            return prefixVoices
        }
        return null
    }

    /**
     * 添加金额音频文件
     */
    fun getMoneyVoice(message: String): MutableList<Int> {
        val voiceStr = readVoice(message)
        val moneyVoices = mutableListOf<Int>()
        for (i in voiceStr.indices) {
            when (voiceStr[i]) {
                in '0'..'9' -> {
                    moneyVoices.add(getNumRaw((voiceStr[i] - 48).toInt()))
                }
                '拾' -> {
                    moneyVoices.add(R.raw.num_10)
                }
                '佰' -> {
                    moneyVoices.add(R.raw.unit_bai)
                }
                '仟' -> {
                    moneyVoices.add(R.raw.unit_qian)
                }
                '万' -> {
                    moneyVoices.add(R.raw.unit_wan)
                }
                '点' -> {
                    moneyVoices.add(R.raw.point)
                }
            }
        }
        return moneyVoices
    }

    /**
     * 通过数字查询播报文件ID
     */
    private fun getNumRaw(num: Int): Int {
        return listOf(
            R.raw.num_0,
            R.raw.num_1,
            R.raw.num_2,
            R.raw.num_3,
            R.raw.num_4,
            R.raw.num_5,
            R.raw.num_6,
            R.raw.num_7,
            R.raw.num_8,
            R.raw.num_9
        )[num]
    }

    /**
     * 将播报内容转换为中文
     * 收款99.99元-->9拾9点99
     */
    private fun readVoice(message: String): String {
        val voiceStr = getMoney(message)
        val result = StringBuffer()
        if (voiceStr.contains(".")) {
            val tmp = voiceStr.split(".")
            result.append(readInt(tmp[0].toInt())).append("点").append(tmp[1])
        } else {
            result.append(readInt(voiceStr.toInt()))
        }
        return result.toString()
    }

    /**
     * 整数转换为中文金额
     */
    private fun readInt(moneyNum: Int): String {
        if (moneyNum == 0) {
            return "0"
        }
        if (moneyNum == 10) {
            return "拾"
        }
        if (moneyNum in 11..19) {
            return "拾${moneyNum % 10}"
        }
        var i = 0
        var tmpMoney = moneyNum
        var result = ""
        while (tmpMoney > 0) {
            result = "${CHINESE_UNIT[i++]}$result"
            result = "${NUM[tmpMoney % 10]}$result"
            tmpMoney /= 10
        }
        return result.replace(Regex("0[拾佰仟]"), "0")
            .replace(Regex("0+亿"), "亿").replace(Regex("0+万"), "万")
            .replace(Regex("0+元"), "元").replace(Regex("0+"), "0")
            .replace(Regex("元"), "")
    }

    /**
     * 收到文字分离出金额
     */
    private fun getMoney(money: String): String {
        val regx = if (money.contains(".")) {
            "(\\d+\\.\\d+)"
        } else {
            "(\\d+)"
        }
        val pattern = Pattern.compile(regx)
        val match = pattern.matcher(money)
        var result = ""
        if (match.find()) {
            result = match.group(1) ?: ""
        }
        return result.replace(".00", "")
    }
}